<?php

namespace Common\Model;
use Think\Model;

class OrderModel extends Model{

    /**
     * 新增或更新
     * 
     * @return boolean fasle 失败 ， int 成功 返回完整的数据
     */
    public function update($data)
    {
        $data = $this->create($data);
        if (empty($data)) {
            return false;
        }
        if (empty($data['order_id'])) {
            $id = $this->add();
            if (! $id) {
                $this->error = '新增出错';
                return false;
            }
        } else {
            $result = $this->where(array( 'order_id' => $data['order_id'] ))->save($data);
            if (false === $result) {
                $this->error = '更新出错';
                return false;
            }
        }
        
        return $data;
    }

    public function getNowOrder($memberID, $menuID)
    {
        if (empty($memberID) || empty($menuID)){
            return false;
        }
//        $now = date('Y-m-d');
//
//        if (time() > strtotime($now . ' 20:00:00')){
//            return false;
//        }

        $menu = M('AreaGoods')->where(array('id' => $menuID))->find();
        if (empty($menu)){
            return false;
        }

        $where= array();
        //$where['menu_id'] = $menuID;
        $where['member_id'] = $memberID;
        $where['pay_state'] = 1;
        $where['create_time'] = array('between', array( $menu['start_sell_time'], $menu['end_sell_time']));
        $result = $this->field('order_id')->where($where)->find();
        return $result;
    }


}
